package com.example.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.lang.Result;
import com.example.entity.Delivery;
import com.example.entity.StuDelivery;
import com.example.service.DeliveryService;
import com.example.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cyk
 * @since 2022-02-19
 */
@RestController
public class DeliveryController {

    @Autowired
    DeliveryService deliveryService;

    /**
     * 根据用户id查询用户的投递记录
     * @param id 用户id
     * @return
     */
    @GetMapping("/stuDelivery/{id}")
    @ResponseBody
    public Result stuDelivery(@PathVariable(name = "id") Long id){

        List<Delivery> list = deliveryService.getStuDelivery(id);

        return Result.succ(list);
    }

    /**
     * 根据 uid 与 rid 添加投递
     * @param uid 用户ID
     * @param rid 招聘信息ID
     * @return
     */
    @GetMapping("/addDelivery/{uid}/{rid}")
    @ResponseBody
    public Result addDelivery(@PathVariable(name = "uid") Long uid,
                              @PathVariable (name = "rid") Long rid){

        Delivery temp = new Delivery();
        temp.setStuId(uid);
        temp.setRecId(rid);
        temp.setStatus(0L);
        temp.setDelTime(LocalDateTime.now());

        // 先查询一下是否已有记录
        Delivery delivery = deliveryService.find(temp);
        // 执行添加操作
        if (delivery == null){
            deliveryService.saveDelivery(temp);
            return Result.succ(200,"操作成功！", null);
        }else {
            return Result.succ(200,"你已提交申请", null);
        }
    }

    /**
     * 删除申请信息
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/delDelivery/{id}")
    public Result delDelivery(@PathVariable(name = "id") Long id){

        Delivery delivery = deliveryService.delDelivery(id);

        return Result.succ(delivery);
    }

    /**
     * 取消申请
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/upDelivery/{id}")
    public Result upDelivery(@PathVariable(name = "id") Long id){

        int i = deliveryService.upDelivery(id);
        if (i > 0){
            return Result.succ(200,"操作成功！",null);
        } else {
            return Result.succ(200,"操作失败！",null);
        }
    }

    /**
     * 同意申请
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/agreeDelivery/{id}")
    public Result agreeDelivery(@PathVariable(name = "id") Long id){

        int i = deliveryService.agreeDelivery(id);
        if (i > 0){
            return Result.succ(200,"操作成功！",null);
        } else {
            return Result.succ(200,"操作失败！",null);
        }
    }

    /**
     * 拒绝申请
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/refuseDelivery/{id}")
    public Result refuseDelivery(@PathVariable(name = "id") Long id){

        int i = deliveryService.refuseDelivery(id);
        if (i > 0){
            return Result.succ(200,"操作成功！",null);
        } else {
            return Result.succ(200,"操作失败！",null);
        }
    }

    /**
     * 企业用户查看投递的信息
     * @param id
     * @param currentPage
     * @return
     */
    @ResponseBody
    @GetMapping("/allStuDelivery/{id}")
    public Result allStuDelivery(@PathVariable(name = "id") Long id,
                                 @RequestParam(defaultValue = "1") Integer currentPage){

        PageUtils page = new PageUtils();
        List<StuDelivery> list = deliveryService.stuDelivery(id);
        IPage pageData = page.getstuDeliveryPage(currentPage,5,list);

        return Result.succ(pageData);
    }

}
